package com.wjf.birdfish.hibernate;

import org.hibernate.Session;

public class PageBuilder<T> {
    private Session session;
    private Class<T> persistentClass;
    private String entryName;
    private String alias;
    private int pageSize;
    private String query;
    private Object[] params;

    public PageBuilder(Session session, String query, Object ... params) {
        this.query = query;
        this.session = session;
        if (params != null) {
        	if (params.length == 0) {
        		params = null;
        	}
        }
        this.params = params;
    }

    public PageBuilder(Class<T> persistentClass, Session session) {
        this.persistentClass = persistentClass;
        this.session = session;
    }

    public PageBuilder(String entryName, Session session) {
        this.entryName = entryName;
        this.session = session;
    }

    public PageBuilder<T> alias(String alias) {
        this.alias = alias;
        return this;
    }

    public PageBuilder<T> pageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public Page<T> create() {
        if (persistentClass != null && alias != null) {
            return new PageImplForCriteria<T>(persistentClass, alias, session, pageSize);
        } else if (persistentClass != null) {
            return new PageImplForCriteria<T>(persistentClass, session, pageSize);
        } else if (entryName != null && alias != null) {
            return new PageImplForCriteria<T>(entryName, alias, session, pageSize);
        } else if (entryName != null) {
            return new PageImplForCriteria<T>(entryName, session, pageSize);
        } else if (query != null) {
            return new PageImplForHql<T>(session, pageSize, query, params);
        }
        return null;
    }
}
